﻿using System;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace YJKRevitFamilyDataBase
{
	public class MySqlDatabase : FamilyLibDatabase
	{
		public ConnectData ConnectData
		{
			get
			{
				return this.mConnectData;
			}
		}

		public override string ConnStr
		{
			get
			{
				return string.Format("Server ={0};Port={1};Charset=utf8;Database ={2}; Uid ={3}; Pwd ={4};", new object[]
				{
					this.mConnectData.IP,
					"3336",
					this.mName,
					this.mConnectData.UserName,
					this.mConnectData.Password
				});
			}
		}

		public MySqlDatabase(string name, ConnectData cd)
		{
			this.mConnectData = cd;
			this.mName = name;
			this.Connection = new MySqlConnection();
			this.Connection.ConnectionString = this.ConnStr;
		}

		public override ISubjectTable SubjectTable
		{
			get
			{
				return new MySqlSubjectTable(this);
			}
		}

		public override ICategoryTable CategoryTable
		{
			get
			{
				return new MySqlCategoryTable(this);
			}
		}

		public override ICategoryParamTable CategoryParamTable
		{
			get
			{
				return new MySqlCategoryParamTable(this);
			}
		}

		public override IFamilyTable FamilyTable
		{
			get
			{
				return new MySqlFamilyTable(this);
			}
		}

		public override IFamilyParamTable FamilyParamTable
		{
			get
			{
				return new MySqlFamilyParamTable(this);
			}
		}

		public override ITypeTable TypeTable
		{
			get
			{
				return new MySqlTypeTable(this);
			}
		}

		public override IFamilyFileTable FamilyFileTable
		{
			get
			{
				return new MySqlFamilyFileTable(this);
			}
		}

		public override IFamilyImgTable FamilyImgTable
		{
			get
			{
				return new MySqlFamilyImgTable(this);
			}
		}

		public static bool TestConnect(string server, string dbName, string uId, string pwd)
		{
			bool result;
			try
			{
				string connectionString = string.Format("Server ={0};Port={1};Charset=utf8;Database ={2}; Uid ={3}; Pwd ={4};", new object[]
				{
					server,
					"3336",
					dbName,
					uId,
					pwd
				});
				MySqlDatabase.tConn.ConnectionString = connectionString;
				MySqlDatabase.tConn.Open();
				MySqlDatabase.tConn.Close();
				MySqlDatabase.tConn.Dispose();
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private ConnectData mConnectData;

		private static SqlConnection tConn = new SqlConnection();
	}
}
